package com.hp.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hp.entity.Patient;
import com.hp.entity.Room;
import com.hp.entity.dto.RoomDto;
import com.hp.service.PatientService;
import com.hp.service.RoomService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "PatientServlet", value = "/patientServlet")
public class PatientServlet extends HttpServlet {
    private ObjectMapper mapper = new ObjectMapper();
    private PatientService patientService = new PatientService();
    private RoomService roomService=new RoomService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String method = request.getParameter("method");
        if (method != null && method.equals("getByIdCard")) {
            getByIdCard(request, response);
        } else if (method != null && method.equals("addPatient")) {
            addPatient(request, response);
        }else if (method != null && method.equals("existsIdMedicare")) {
            existsIdMedicare(request, response);
        }else if (method != null && method.equals("getRoom")) {
            getRoom(request, response);
        }

    }
    //查询医生排期房间号
    private void getRoom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        String sign = request.getParameter("sign");
        String departmentId = request.getParameter("departmentId");
        String userId = request.getParameter("userId");
        RoomDto room = roomService.getRoom(sign, departmentId, userId);
        if (room != null) {
            map.put("room", room);
        } else {
            map.put("msg", "该医生没有门诊权限");
        }
        String json = mapper.writeValueAsString(map);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        response.getWriter().print(json);
    }
    //添加病人
    private void addPatient(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        Patient patient = new Patient();
        try {
            //日期格式化
            DateConverter converter = new DateConverter();
            converter.setPattern(new String("yyyy-MM-dd"));
            ConvertUtils.register(converter,Date.class);
            //填充数据到实体类
            BeanUtils.populate(patient, request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //设置其他字段值
        Date currentDate = new Date();
        patient.setCreatedTime(currentDate);
        patient.setUpdatedTime(currentDate);
        //添加用户
        if (patientService.addPatient(patient)) {
            map.put("code", 1006);
            map.put("msg", "创建就诊卡成");
        } else {
            map.put("code", 1007);
            map.put("msg", "创建就诊卡失败");
        }
        String json = mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }
    //验证身份证
    private void getByIdCard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        String idCard = request.getParameter("idCard");
        Patient patient = null;
        patient = patientService.getPatientByIdCard(idCard);
        if (patient != null) {
            map.put("code", 1001);
            //方便挂号查询病人信息
            map.put("patient", patient);
        } else {
            map.put("code", 1002);
            map.put("msg", "身份证号有效，可以进行注册");
        }
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        String json = mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }
    //验证医保卡重复
    private void existsIdMedicare(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        String idMedicare = request.getParameter("idMedicare");
        boolean result = patientService.existsIdMedicare(idMedicare);
        if (!result) {
            map.put("code", 1001);
        } else {
            map.put("code", 1002);
        }
        String json = mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
